﻿using log4net;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace LogCentre
{
    public delegate void UIMessageEventHandler(string info, MessageType type);

    public enum MessageType
    {
        INFO,
        ERROR,
        DEBUG
    }

    public class MainLog
    {
        private static readonly ILog logger = LogManager.GetLogger("mainLogger");
        public static event UIMessageEventHandler UiLogReceived;
   
        static MainLog()
        {
            //var path = @"C:\PGL\Config\log4net_config.xml";
            var path = AppDomain.CurrentDomain.BaseDirectory + @"Config\log4net_config.xml";

            //string path = new DirectoryInfo(string.Format(@"{0}..\Config\log4net_config.xml", AppDomain.CurrentDomain.BaseDirectory)).FullName;

            log4net.Config.XmlConfigurator.Configure(new FileInfo(path));

            //log4net.Config.XmlConfigurator.Configure();
        }

        public static void Info(string msg, bool isUiDisplayed)
        {
            logger.Info(msg);
            if (isUiDisplayed)
            {
                UiLogReceived?.Invoke(msg, MessageType.INFO);
            }
        }

        public static void Info(string msg)
        {
            if (outMsg != msg)
            {
                logger.Info(msg);
                if (UiLogReceived != null)
                {
                    UiLogReceived(msg, MessageType.INFO);
                }
                //outInfo = msg;
            }
        }

        public static void Warn(string msg)
        {
            //while (!mainLogger.IsWarnEnabled)
            //{
            //    Task.Delay(1);
            //}
            logger.Warn(msg);
        }

        static string outMsg = string.Empty;
        public static void Error(string msg)
        {
            //while (!mainLogger.IsErrorEnabled)
            //{
            //    Task.Delay(1);
            //}

            logger.Error(msg);

            //if (outMsg != msg)
            //{
            //    StackTrace stackTrace = new StackTrace();
            //    StackFrame stackFrame = stackTrace.GetFrame(1);
            //    MethodBase methodBase = stackFrame.GetMethod();
            //    lErr.Error("类名:" + methodBase.ReflectedType.FullName + " 方法名:" + methodBase.Name + " 信息:" + msg);
            //    if (msg.Contains("System.OutOfMemoryException"))
            //    {
            //        UiLogReceived(msg, MessageType.ERROR);
            //        //UiLogReceived(null, new UiLogEventArgs(msg));
            //    }
            //    outMsg = msg;
            //}
        }

        public static void Error(string msg, bool isUiDisplayed)
        {
            logger.Error(msg);
            if (isUiDisplayed)
            {
                UiLogReceived(msg, MessageType.ERROR);
            }
        }

        public static void Error(string msg, bool isUiDisplayed, Exception ex)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" " + msg);
            sb.Append(" " + ex.ToString());
            logger.Error(sb.ToString());
            if (isUiDisplayed)
            {
                UiLogReceived(msg, MessageType.ERROR);
            }
        }

        public static void Error(Exception ex)
        {
            StringBuilder sb = new StringBuilder();
            if (ex.Message != null)
                sb.Append(" Message:" + ex.Message);
            if (ex.InnerException != null)
                sb.Append(" InnerException " + ex.InnerException);
            sb.Append(" " + ex.ToString());
            Error(sb.ToString());
        }

        public static void Debug(string msg, bool isUiDisplayed)
        {
            logger.Debug(msg);
            if (isUiDisplayed)
            {
                UiLogReceived?.Invoke(msg, MessageType.DEBUG);
            }
        }

        public class UiLogEventArgs : EventArgs
        {
            public string Message { get; private set; }
            public UiLogEventArgs(string message)
            {
                Message = message;
            }
        }

        public static void Debug(string msg)
        {
            logger.Debug(msg);
        }
    }
}
